const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        mobile: '',
        captcha: '',
        password: ''
    },
    onLoad: function (options) {
        this.setData({
            mobile: options.mobile,
            captcha: options.captcha,
        });
    },
    passwordInput: function (event) {
        this.setData({
            password: event.detail.value,
        });
    },

    /**
     * 提交短信验证码  下一步
     */
    commitCode: function (event) {
        var self = this;
        if (!self.data.password) {
            wx.showToast({
                title: '请输入新密码',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=connect&op=find_password',
            method: "POST",
            data: {
                password: self.data.password,
                phone: self.data.mobile,
                captcha: self.data.captcha,
                client: "wap"
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
                if (res.data.code == 200) {
                    wx.setStorage({
                        key: 'key',
                        data: res.data.datas.key
                    })
                    wx.setStorage({
                        key: 'username',
                        data: res.data.datas.username
                    })

                    self.getCartNum(res.data.datas.key);
                    wx.switchTab({
                        url: '../member/member'
                    })

                } else {
                    wx.showToast({
                        title: res.data.datas.error,
                        icon: 'none'
                    })
                }
            },
            fail: function (res) {
                wx.showToast({
                    title: '网络请求失败',
                    icon: 'none'
                })
            }
        })
    },
    //获取购物车商品数
    getCartNum: function (key) {
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=member_cart&op=cart_goods_count',
            data: {
                key: key
            },
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            dataType: 'json',
            success: (res) => {
                if (res.data.code == 200) {
                    var result = res.data.datas;
                    wx.setTabBarBadge({
                        index: 3,
                        text: String(result.cart_count)
                    })
                    wx.setStorage({
                        key: 'cart_count',
                        data: result.cart_count
                    })
                }
            }
        });
    }
})